/*global Qiniu */
/*global plupload */
/*global FileProgress */
/*global hljs */

$(function() {
    var sendData = {
        bucket:'atphotos'
    }
    server.getTokenData(sendData,'http://www.atphotos.cn/atphotos/upload/getTokenWithCallback',function(res){
        // 上传图片
        upImg(res);
    })
    function upImg(res){
        var uploader = Qiniu.uploader({
            runtimes: 'html5,flash,html4',
            browse_button: 'pickfiles',
            max_file_size: '100mb',
            get_new_uptoken: false,
            uptoken:res,
            domain: 'z2',
            auto_start: true,
            init: {
                'FilesAdded': function(up, files) {
                    plupload.each(files, function(file) {
                        console.log('filetype: ' + file.type);
                        if(file.type=='image/jpeg'||file.type=='image/jpg'||file.type=='image/png'||file.type=='image/gif' || file.type=='video/x-matroska' || file.type=='video/mp4'){
                            console.log('type:' + file.type);
                            isUpload =true;
                            // file.album_name=album_name;
                            var progress = new FileProgress(file, 'fsUploadProgress');
                            progress.setStatus("等待...");
                            progress.bindUploadCancel(up);
                        }else {
                            isUpload = false;
                            up.removeFile(file);
                            console.log('上传类型只能是.jpg,.png,.gif,.mkv');
                            return false;
                        }});
                },
                'BeforeUpload': function(up, file) {
                    console.log("this is a beforeupload function from init");
                    var progress = new FileProgress(file, 'fsUploadProgress');
                },
                'UploadProgress': function(up, file) {
                    var progress = new FileProgress(file, 'fsUploadProgress');
                },
                'UploadComplete': function() {
                    $('#success').show();
                },
                'FileUploaded': function(up, file, info) {
                    var progress = new FileProgress(file, 'fsUploadProgress');
                    progress.setComplete(up, info);
                },
                'Error': function(up, err, errTip) {
                    $('table').show();
                    var progress = new FileProgress(err.file, 'fsUploadProgress');
                    progress.setError();
                    progress.setStatus(errTip);
                }

                // ,
                // 'Key': function(up, file) {
                //     var key = "";
                //     // do something with key
                //     return key
                // }
            }
        });
    }
    $('#up_load').on('click', function(){
        uploader.start();
    });
    $('#stop_load').on('click', function(){
        uploader.stop();
    });
    $('#retry').on('click', function(){
        uploader.stop();
        uploader.start();
    });

});
